import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QHBoxLayout, \
    QRadioButton, QMessageBox
import requests
import re


class Register(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.parent = parent
        self.client=self.parent.client
        self.resize(450, 700)
        self.setWindowTitle("创建新账户")
        icon = QIcon(r'./static/img/商城图标.png')
        self.setWindowIcon(icon)
        self.initUI()

    def initUI(self):
        mainLayout = QVBoxLayout(self)
        input_style = """
            padding-left: 10px;
            border-radius: 15px;
        """
        Text1 = QLabel('创建新用户')
        Text1.setStyleSheet("""
            font-size: 40px;
            font-weight: bold;
        """)
        Text1.setAlignment(Qt.AlignCenter)
        mainLayout.addWidget(Text1)

        Text2 = QLabel('加入我们,开始新的购物之旅')
        Text2.setStyleSheet("""
            font-size: 15px;
            color: gray;
        """)
        Text2.setAlignment(Qt.AlignCenter)
        mainLayout.addWidget(Text2)

        text_name = QLabel('用户名')
        text_name.setStyleSheet("""
            font-size: 20px;
            font-weight: bold;
            padding-left: 10px;
            color: #1D3A50;
        """)
        mainLayout.addWidget(text_name)

        self.name_input = QLineEdit()
        self.name_input.setStyleSheet(input_style)
        self.name_input.setPlaceholderText('请输入用户名')
        self.name_input.setMinimumHeight(40)
        mainLayout.addWidget(self.name_input)

        text_emil = QLabel('邮箱')
        text_emil.setStyleSheet("""
            font-size: 20px;
            font-weight: bold;
            padding-left: 10px;
            color: #1D3A50;
        """)
        mainLayout.addWidget(text_emil)

        self.Emil_input = QLineEdit()
        self.Emil_input.setStyleSheet(input_style)
        self.Emil_input.setPlaceholderText('请输入邮箱')
        self.Emil_input.setMinimumHeight(40)
        mainLayout.addWidget(self.Emil_input)

        text_pwd = QLabel('密码')
        text_pwd.setStyleSheet("""
            font-size: 20px;
            font-weight: bold;
            padding-left: 10px;
            color: #1D3A50;
        """)
        mainLayout.addWidget(text_pwd)

        self.pwd_input = QLineEdit()
        self.pwd_input.setStyleSheet(input_style)
        self.pwd_input.setPlaceholderText('请输入密码')
        self.pwd_input.setEchoMode(QLineEdit.Password)
        self.pwd_input.setMinimumHeight(40)
        mainLayout.addWidget(self.pwd_input)

        text_spwd = QLabel('确认密码')
        text_spwd.setStyleSheet("""
            font-size: 20px;
            font-weight: bold;
            padding-left: 10px;
            color: #1D3A50;
        """)
        mainLayout.addWidget(text_spwd)

        self.pwds_input = QLineEdit()
        self.pwds_input.setStyleSheet(input_style)
        self.pwds_input.setPlaceholderText('请再次输入密码')
        self.pwds_input.setEchoMode(QLineEdit.Password)
        self.pwds_input.setMinimumHeight(40)
        mainLayout.addWidget(self.pwds_input)

        text_lx = QLabel('账户类型')
        text_lx.setStyleSheet("""
                    font-size: 20px;
                    font-weight: bold;
                    padding-left: 10px;
                    color: #1D3A50;
                """)
        mainLayout.addWidget(text_lx)
        select_layout = QHBoxLayout()
        self.button1 = QRadioButton('🛒普通用户')
        self.button1.setStyleSheet("""
            padding-left: 80px;
        """)
        self.button2 = QRadioButton('👨管理者')  # self.sender
        select_layout.addWidget(self.button1)
        select_layout.addWidget(self.button2)
        mainLayout.addLayout(select_layout)
        submit_btn = QPushButton('创建账户')
        exit_btn = QPushButton('取消')
        submit_btn.setMinimumHeight(40)
        exit_btn.setMinimumHeight(40)
        submit_btn.setStyleSheet("""
            QPushButton{
                background: #4698BB;
                color: white;
                font-size: 20px;
                font-weight: bold;
                border-radius: 15px;
            }
            QPushButton:hover{
                background: #74B7D3;
            }
            QPushButton:pressed{
                background: #4698BB;
                padding-left: 10px;
                padding-top: 10px;
            }
        """)
        exit_btn.setStyleSheet("""
            QPushButton{
                background: #D4EAEB;
                color: gray;
                font-size: 20px;
                font-weight: bold;
                border-radius: 20px;
                border:2px solid gray;


            }
            QPushButton:hover{
                background: #E6F3F3;
            }
            QPushButton:pressed{
                background: #D4EAEB;
                padding-left: 10px;
                padding-top: 10px;
            }
        """)
        mainLayout.addWidget(submit_btn)
        mainLayout.addWidget(exit_btn)
        submit_btn.clicked.connect(self.submit)
        exit_btn.clicked.connect(self.exit)

    def submit(self):
        user_name = self.name_input.text()
        email = self.Emil_input.text()
        pwd = self.pwd_input.text()
        pwd_again = self.pwds_input.text()
        if not user_name or not email or not pwd or not pwd_again:
            QMessageBox.critical(None, '错误', '请将信息填写完整')
            return
        if self.button1.isChecked():
            is_admin = False
        elif self.button2.isChecked():
            is_admin = True
        else:
            QMessageBox.critical(None, '错误', '请选择你的身份')
            return
        if pwd != pwd_again:
            QMessageBox.critical(None, '错误', '两次输入的密码不一致')
            return
        if len(pwd) < 8:
            QMessageBox.critical(None, '错误', '密码需要大于8位')
            return
        content = re.findall(r'[0-9a-zA-Z]+@[0-9a-zA-Z]+\.com', email)
        if not content:
            QMessageBox.critical(None, '错误', '邮箱不符合规范')
            return
        parser = {
            'username': user_name,
            'email': email,
            'password': pwd,
            'is_admin': is_admin,
        }
        statu=self.client.register(parser)
        if statu:
            QMessageBox.information(None, '提示', '注册成功')
        else:
            QMessageBox.critical(None, '错误', '注册失败，用户名已存在')
        self.name_input.clear()
        self.Emil_input.clear()
        self.pwd_input.clear()
        self.pwds_input.clear()

    def exit(self):
        if self.parent:
            self.parent.show()
        self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Register()
    window.show()
    sys.exit(app.exec_())